package com.example.sqlitetest.Activity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.widget.ListView;

import com.example.sqlitetest.Adapter.LiuYanAdapter;
import com.example.sqlitetest.Adapter.StudntsAdapter;
import com.example.sqlitetest.R;
import com.example.sqlitetest.db.MyDataBaseHelper;
import com.example.sqlitetest.model.LiuYanInfo;
import com.example.sqlitetest.model.StudentInfo;

import java.util.ArrayList;
import java.util.List;

public class LiuYanListActivity extends AppCompatActivity {
    //定义数据
    private List<LiuYanInfo> mData;
    private MyDataBaseHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liuyanlist);



        //操作list列表
        final ListView listView = (ListView) findViewById(R.id.list);
        LayoutInflater inflater = getLayoutInflater();

        //初始化数据库对象
        helper = new MyDataBaseHelper(this);//创建并初始化DBOpenHelper对象
        geLiuYanInfoList();

        //实例化ListAdapter
        LiuYanAdapter adapter = new LiuYanAdapter(this, inflater, mData);
        listView.setAdapter(adapter);


    }
    /**
     * 获得数据库中的数据
     */
    private List<LiuYanInfo> geLiuYanInfoList() {
        mData = new ArrayList<LiuYanInfo>();
        SQLiteDatabase sqLiteDatabase = helper.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("liuyan", null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                String name = "留言名称："+cursor.getString(cursor.getColumnIndex("username"));
                String liuyan = "留言内容："+cursor.getString(cursor.getColumnIndex("liuyan"));

                mData.add(new LiuYanInfo(name,liuyan));
            } while (cursor.moveToNext());
        }
        cursor.close();
        return mData;
    }
}

